package com.tanner.mapreducedemo.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Before;
import org.junit.Test;

/**
 * @author zhang.t.c
 * @date 2021/7/31
 */
public class HbaseTest {
//    Configuration config = null;
//    Connection connection = null;
//    Admin admin = null;
//
//    public static final String NAMESPACE = "ztc";
//    public static final String TABLE_NAME = "student";
//
//    @Before
//    public void init() {
//        config = HBaseConfiguration.create();
//        // 如果是集群 则主机名用逗号分隔
//        config.set("hbase.zookeeper.quorum", "47.101.204.23");
//        config.set("hbase.zookeeper.property.clientPort", "2181");
//
//        try {
//            connection = ConnectionFactory.createConnection(config);
//            admin = connection.getAdmin();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    @Test
    public void createNamespace() throws Exception {
        HbaseUtils.createNamespace("zhangtiancheng");
    }

    @Test
    public void createTable() throws Exception {
        HbaseUtils.createTable("zhangtiancheng:student", new String[]{"info", "score"});
    }

    @Test
    public void insertData() throws Exception {
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Tom","info",
                new String[]{"student_id","class"},
                new String[]{"20210000000001","1"});
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Tom","score",
                new String[]{"understanding","programming"},
                new String[]{"75","82"});

        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Jerry","info",
                new String[]{"student_id","class"},
                new String[]{"20210000000002","1"});
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Jerry","score",
                new String[]{"understanding","programming"},
                new String[]{"85","67"});

        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Jack","info",
                new String[]{"student_id","class"},
                new String[]{"20210000000003","2"});
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Jack","score",
                new String[]{"understanding","programming"},
                new String[]{"80","80"});

        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Rose","info",
                new String[]{"student_id","class"},
                new String[]{"20210000000004","2"});
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","Rose","score",
                new String[]{"understanding","programming"},
                new String[]{"60","61"});

        HbaseUtils.insertOrUpdate("zhangtiancheng:student","ztc","info",
                new String[]{"student_id","class"},
                new String[]{"G20210735010309","4"});
        HbaseUtils.insertOrUpdate("zhangtiancheng:student","ztc","score",
                new String[]{"understanding","programming"},
                new String[]{"100","100"});
    }

    @Test
    public void scanTable() throws Exception {
        HbaseUtils.scanTable("zhangtiancheng:student", null, null, null);
    }

}
